<?php

namespace app\forms\position;

use yii\db\Exception;
use app\forms\Model;
use app\models\Position;
use app\validators\PositiveInteger;

class DefaultForm extends Model
{
    public $id;

    public function rules()
    {
        return [
            ['id', PositiveInteger::class],
            ['id', 'required'],
            ['id', 'exist', 'targetClass' => Position::class],
        ];
    }

    public static function getDefault($param)
    {
        $form = new self;
        $form->setAttributes($param);
        !$form->validate() ? $form->validateError() : '';

        try {
            return (new Position())->getDefaultItem($form['id']);
        } catch (Exception $e) {
            $form->serverError();
        }
    }
}